// Unless explicitly stated otherwise all files in this repository are licensed
// under the MIT License.
// This product includes software developed at Guance Cloud (https://www.guance.com/).
// Copyright 2021-present Guance, Inc.

package main

import (
	"fmt"
	"orcmetric/collect"
	"path/filepath"

	"github.com/GuanceCloud/cliutils/logger"
	_ "github.com/godror/godror"
	"github.com/jessevdk/go-flags"
)

var (
	opt collect.Option
	l   = logger.DefaultSLogger("oracle")
)

func main() {
	// fmt.Println("wait for ENTER...")
	// fmt.Scanln()
	// fmt.Println("found ENTER, go on")

	// // /home/oracle/oracle --interval $DATAKIT_INTERVAL --host 0.0.0.0 --port 1521 --username datakit --password 123456 --service-name $ORACLE_SID --datakit-http-host $DATAKIT_HOST --datakit-http-port $DATAKIT_PORT
	// opt = Option{
	// 	Interval: "3s",
	// 	// Metric:   "oracle_monitor",
	// 	// InstanceDesc:,
	// 	Host:        "localhost",
	// 	Port:        "1521",
	// 	Username:    "datakit",
	// 	Password:    "123456",
	// 	ServiceName: "XE",
	// 	// Tags:,
	// 	// DatakitHTTPHost:,
	// 	// DatakitHTTPPort:,
	// 	Election: false,
	// 	// Log:      "/root/download/staff_853/docker_script/oracle/19c/orcmetric/oracle.log",
	// 	LogLevel: "debug",
	// 	// Query:,
	// }

	// _, err := flags.Parse(&opt)
	// if err != nil {
	// 	fmt.Println("Parse error:", err)
	// 	return
	// }

	// if opt.Log == "" {
	// 	// opt.Log = filepath.Join("/usr/local/datakit", "externals", "oracle.log")
	// 	opt.Log = "/root/download/staff_853/docker_script/oracle/19c/orcmetric/oracle.log"
	// }

	_, err := flags.Parse(&opt)
	if err != nil {
		fmt.Println("Parse error:", err)
		return
	}

	if opt.Log == "" {
		opt.Log = filepath.Join("/usr/local/datakit", "externals", "oracle.log")
	}

	if err := logger.InitRoot(&logger.Option{
		Path:  opt.Log,
		Level: opt.LogLevel,
		Flags: logger.OPT_DEFAULT,
	}); err != nil {
		l.Errorf("set root log failed: %s", err.Error())
	}

	if opt.InstanceDesc != "" { // add description to logger
		l = logger.SLogger("oracle-" + opt.InstanceDesc)
	} else {
		l = logger.SLogger("oracle")
	}

	l.Debugf("election: %t", opt.Election)

	l.Infof("datakit: host=%s, port=%d", opt.DatakitHTTPHost, opt.DatakitHTTPPort)

	collect.Set(&opt, l)

	m := collect.NewMonitor()
	m.Run()
}
